# 不使用框架和封装技术实现员工模块的增删改查

# 1 导包
import requests
import app

# 2 调用登陆
response = requests.post(url="http://ihrm-test.itheima.net/api/sys/login",
                         headers={"Content-Type": "application/json"},
                         json={"mobile": "13800000002", "password": "123456"})
print("登陆的结果为：", response.json())
# 3 提取登陆返回的令牌
token = "Bearer " + response.json().get('data')
print("保存全局变量之前的请求头：---", app.HEADERS)
app.HEADERS['Authorization'] = token  # 把令牌保存到全局变量的请求头当中
print("查看保存的全局变量请求头：", app.HEADERS)

# 4 使用提取的令牌，实现查询员工列表接口
response = requests.get(url=app.BASE_URL + "/api/sys/user?page=1&size=10",
                        headers=app.HEADERS)
print("查询员工列表的结果为：", response.json())
# 5 实现添加员工接口
response = requests.post(url=app.BASE_URL + "/api/sys/user",
                         headers=app.HEADERS,
                         json={"username": "丫头",
                               "mobile": "1321914806",
                               "timeOfEntry": "2020-11-03",
                               "formOfEmployment": 1,
                               "workNumber": "1111",
                               "departmentName": "9",
                               "departmentId": "1322185740402130944",
                               "correctionTime": "2020-11-22T16:00:00.000Z"})
print("添加员工的结果为：", response.json())
# 6 提取员工ID
emp_id = response.json().get("data").get("id")  # 提取员工ID
print("保存到全局之前，全局变量员工ID为：", app.EMP_ID)
app.EMP_ID = emp_id  # 保存提取的id到全局变量
print("全局变量员工ID为：", app.EMP_ID)

# 7 使用提取的员工ID，查询员工
response = requests.get(url=app.BASE_URL + "/api/sys/user" + "/" + app.EMP_ID,
                        headers=app.HEADERS)
print("打印查询员工的结果：", response.json())
print("查询的员工的URL为：", response.url)

# 8 修改员工
response = requests.put(url=app.BASE_URL + "/api/sys/user" + "/" + app.EMP_ID,
                        headers=app.HEADERS,
                        json={"username": "穿山甲"})
print("修改员工的结果为：", response.json())

# 9 删除员工
response = requests.delete(url=app.BASE_URL + "/api/sys/user" + "/" + app.EMP_ID,
                           headers=app.HEADERS)
print("删除员工的结果为：", response.json())
